<template>
  <div class="editnotice">
    <div class="header">
      <table>
        <tr>
          <td>
            <span style="margin-left: 20px">服务器选择：</span>
            <el-select
              @change="selectServer"
              v-model="server"
              placeholder="请选择服务器"
            >
              <el-option
                v-for="item in banbenarr"
                :label="item.label"
                :key="item.value"
                :value="item.value"
              ></el-option>
            </el-select>
          </td>
          <td>
            <span style="margin-left: 20px">请选择tab标签：</span>
            <el-select v-model="tabs" placeholder="请选择tab">
              <el-option
                v-for="item in tab"
                :label="item.label"
                :key="item.value"
                :value="item.value"
              ></el-option>
            </el-select>
          </td>
          <td>
            <el-button type="primary" size="small" @click="search()"
            >公告查询</el-button>
            <el-button type="warning" size="small" @click="deleted"
            >删除公告</el-button>
          </td>
        </tr>
      </table>
    </div>
    <div class="content">
      <el-main v-loading="loading" style="width: 100%; height: auto">
        <el-form ref="MailForm" :model="MailForm" label-width="120px">
          <el-form-item label="请选择公告状态:">
            <el-select v-model="MailForm.str1" placeholder="请选择公告状态">
              <el-option label="弹出公告" value="NEW"></el-option>
              <el-option label="不弹公告" value="OLD"></el-option>
              <el-option label="停服维护" value="CLOSE"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="中文页签:">
            <el-input
              v-model="MailForm.cnName"
              placeholder="请输入中文页签"
            ></el-input>
          </el-form-item>
          <el-form-item label="中文内容:">
            <el-input
              :autosize="{ minRows: 6 }"
              type="textarea"
              v-model="MailForm.cnContent"
              placeholder="请输入中文内容"
            ></el-input>
          </el-form-item>
          <el-form-item label="英文页签:">
            <el-input
              v-model="MailForm.enName"
              placeholder="请输入英文页签"
            ></el-input>
          </el-form-item>
          <el-form-item label="英文内容:">
            <el-input
              :autosize="{ minRows: 6 }"
              type="textarea"
              v-model="MailForm.enContent"
              placeholder="请输入英文内容"
            ></el-input>
          </el-form-item>
          <el-form-item label="法文页签:">
            <el-input
              v-model="MailForm.frenchName"
              placeholder="请输入法文页签"
            ></el-input>
          </el-form-item>
          <el-form-item label="法文内容:">
            <el-input
              :autosize="{ minRows: 6 }"
              type="textarea"
              v-model="MailForm.frenchContent"
              placeholder="请输入法文内容"
            ></el-input>
          </el-form-item>
          <el-form-item label="德文页签:">
            <el-input
              v-model="MailForm.germanName"
              placeholder="请输入德文页签"
            ></el-input>
          </el-form-item>
          <el-form-item label="德文内容:">
            <el-input
              :autosize="{ minRows: 6 }"
              type="textarea"
              v-model="MailForm.germanContent"
              placeholder="请输入德文内容"
            ></el-input>
          </el-form-item>
          <el-form-item label="俄文页签:">
            <el-input
              v-model="MailForm.russianName"
              placeholder="请输入俄文页签"
            ></el-input>
          </el-form-item>
          <el-form-item label="俄文内容:">
            <el-input
              :autosize="{ minRows: 6 }"
              type="textarea"
              v-model="MailForm.russianContent"
              placeholder="请输入俄文内容"
            ></el-input>
          </el-form-item>
        </el-form>
        <el-button
          type="primary"
          size="small"
          style="margin-left: 100px; width: 10%"
          @click="onSubmit"
        >
          公告保存
        </el-button>
      </el-main>
    </div>
  </div>
</template>
<script>
import notice_server from "../../config/notice_server";
import notice_tap from "../../config/notice_tap"
import {getTime} from "../../utils/index"
export default {
  data() {
    return {
      server: "",
      tabs: "",
      noticeArr: [],
      loading: false,
      banbenarr: [],
      tab: notice_tap,
      MailForm: {
        cnName: "",
        cnContent: "",
        enName: "",
        enContent: "",
        frenchName: "",
        frenchContent: "",
        germanName: "",
        germanContent: "",
        russianName: "",
        russianContent: "",
        str1: "",
      },
    };
  },
  mounted() {
    this.banbenarr = notice_server;
  },
  methods: {
    //获取公告中str信息
    selectServer() {
      this.$http.post("/api/gmnotice/getOssStr", {
          server: this.server,
          tabs: this.tabs,
      }).then((res) => {
          let obj = res.data.result;
          this.MailForm.str1 = obj.str1;
      });
    },
    //查询公告
    search() {
      if (this.server == "" || this.tabs == "") {
        this.$message({type:"warning",message:"请选择服务器和公告页面中的需要编辑的Tab标签"});
        return;
      } else {
        this.loading = true;
        this.$http.post("/api/gmnotice/getOss", {
            server: this.server,
            tabs: this.tabs,
            user_actions: "公告查询",
            date: getTime(new Date()),
        }).then((res) => {
            if (res.data.code == 200) {
              let obj = res.data.result;
              this.MailForm.cnName = obj.cnName;
              this.MailForm.cnContent = obj.cnContent;
              this.MailForm.enName = obj.enName;
              this.MailForm.enContent = obj.enContent;
              this.MailForm.frenchName = obj.frenchName;
              this.MailForm.frenchContent = obj.frenchContent;
              this.MailForm.germanName = obj.germanName;
              this.MailForm.germanContent = obj.germanContent;
              this.MailForm.russianName = obj.russianName;
              this.MailForm.russianContent = obj.russianContent;
              this.loading = false;
            } else {
              this.$message({message: res.data.msg,type: 'warning'});
              this.loading = false;
              this.MailForm.cnName = "";
              this.MailForm.cnContent = "";
              this.MailForm.enName = "";
              this.MailForm.enContent = "";
              this.MailForm.frenchName = "";
              this.MailForm.frenchContent = "";
              this.MailForm.germanName = "";
              this.MailForm.germanContent = "";
              this.MailForm.russianName = "";
              this.MailForm.russianContent = "";
              return;
            }
          });
      }
    },
    //删除公告
    deleted() {
        if (this.server == "" || this.tabs == "") {
          this.$message({type:"warning",message:"请选择服务器和Tab标签"});
          return;
        } else {
          this.$confirm("确定删除公告吗?", "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          }).then(() => {
              var Name = {
                CHmessage: "",
                ENmessage: "",
                FRmessage: "",
                DEmessage: "",
                RUmessage: "",
              };
              var Content = {
                CHmessage: "",
                ENmessage: "",
                FRmessage: "",
                DEmessage: "",
                RUmessage: "",
              };
              let tab = {
                Name: Name,
                Content: Content,
              };
              this.$http.post("/api/gmnotice/delOss", {
                  Tab: this.tabs,
                  tab: tab,
                  server: this.server,
                  user_actions: "公告删除",
                  date: getTime(new Date()),
              }).then((res) => {
                  if (res.data.code == 200) {
                    this.tabs = "";
                    this.server = "";
                    this.MailForm.cnName = "";
                    this.MailForm.cnContent = "";
                    this.MailForm.enName = "";
                    this.MailForm.enContent = "";
                    this.MailForm.frenchName = "";
                    this.MailForm.frenchContent = "";
                    this.MailForm.germanName = "";
                    this.MailForm.germanContent = "";
                    this.MailForm.russianName = "";
                    this.MailForm.russianContent = "";
                  }
                  this.$message({type:"success",message:"公告删除成功"});
                  this.$router.go(0)
                });
            }).catch(() => {
              return;
            });
        }
    },
    //公告保存
    onSubmit() {
        let obj = {};
        if (this.MailForm.str1 == "" || this.MailForm.str1 == undefined) {
          this.$message({message: '请输入公告类型！',type: 'warning'});
          return;
        } else if (this.MailForm.cnName == "" || this.MailForm.cnName == undefined) {
          this.$message({message: '请输入中文页签！',type: 'warning'});
          return;
        } else if (this.MailForm.enName == "" || this.MailForm.enName == undefined) {
          this.$message({message: '请输入英文页签！',type: 'warning'});
          return;
        } else if (this.MailForm.frenchName == "" || this.MailForm.frenchName == undefined) {
          this.$message({message: '请输入法文页签',type: 'warning'});
          return;
        } else if (this.MailForm.germanName == "" || this.MailForm.germanName == undefined) {
          this.$message({message: '请输入德文页签',type: 'warning'});
          return;
        } else if (this.MailForm.russianName == "" || this.MailForm.russianName == undefined) {
          this.$message({message: '请输入俄文页签',type: 'warning'});
          return;
        } else if (this.MailForm.cnContent == "" || this.MailForm.cnContent == undefined) {
          this.$message({message: '请输入中文内容',type: 'warning'});
          return;
        } else if (this.MailForm.enContent == "" || this.MailForm.cnName == undefined) {
          this.$message({message: '请输入英文内容',type: 'warning'});
          return;
        } else if (this.MailForm.frenchContent == "" || this.MailForm.frenchContent == undefined) {
          this.$message({message: '请输入法文内容',type: 'warning'});
          return;
        } else if (this.MailForm.germanContent == "" || this.MailForm.germanContent == undefined) {
          this.$message({message: '请输入德文内容',type: 'warning'});
          return;
        } else if (this.MailForm.russianContent == "" || this.MailForm.russianContent == undefined) {
          this.$message({message: '请输入俄文内容',type: 'warning'});
          return;
        } else {
          var Name = {
            CHmessage: this.MailForm.cnName,
            ENmessage: this.MailForm.enName,
            FRmessage: this.MailForm.frenchName,
            DEmessage: this.MailForm.germanName,
            RUmessage: this.MailForm.russianName,
          };
          var Content = {
            CHmessage: this.MailForm.cnContent,
            ENmessage: this.MailForm.enContent,
            FRmessage: this.MailForm.frenchContent,
            DEmessage: this.MailForm.germanContent,
            RUmessage: this.MailForm.russianContent,
          };
          obj = {
            Name: Name,
            Content: Content,
          };
        }
        if (this.server == "" || this.tabs == "") {
          this.$message({message: '请选择服务器和需要添加的Tab标签',type: 'warning'});
          return;
        } else {
          this.$http.post("/api/gmnotice/saveOss", {
              tabs: this.tabs,
              str1: this.MailForm.str1,
              obj: obj,
              server: this.server,
              user_actions: "公告保存",
              date: getTime(new Date()),
            }).then((res) => {
              if (res.data.code == 200) {
                this.$message.success(res.data.msg);
                this.tabs = "";
                this.server = "";
                this.MailForm = {};
                this.$message({type:"success",message:"公告保存成功"});
                this.$router.go(0)
              } else {
                this.$message({type:"warning",message:"公告保存失败"});
              }
          });
        }
    },
  },
};
</script>
<style scoped>
.editnotice {
  width: 100%;
  height: auto;
  background-color: #fff;
}
.el-button {
  margin: 20px 20px;
}
</style>